package com.et114.modules.system.dictionary.service.impl;


import java.util.List;

import com.et114.core.exception.CheckException;
import com.et114.core.utility.Validator;
import com.et114.modules.system.dictionary.BizEntity;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.IDictionaryDao;
import com.et114.modules.system.dictionary.service.IDictionaryService;
import com.et114.modules.system.dictionary.vo.BizOption;
import com.et114.modules.system.dictionary.vo.BizType;

public class DictionaryServiceImpl implements IDictionaryService {
	
	private IDictionaryDao	dictionaryDao;
	
	public IDictionaryDao getDictionaryDao ( ) {
		return dictionaryDao;
	}
	
	public void setDictionaryDao ( IDictionaryDao dictionaryDao ) {
		this.dictionaryDao = dictionaryDao;
	}
	
	public int addBizOption ( BizOption bizOption ) throws Exception {
		// TODO Auto-generated method stub
		int result = dictionaryDao.addBizOption ( bizOption );
		List ops = retrieveBizOptionList ( bizOption.getBizTypeKey ( ) , DictionaryCache.BIZOPTION_STATUS_ALL ) ;
		BizType biztype = dictionaryDao.getBizTypeByKey(bizOption.getBizTypeKey());
		biztype.setOptions(ops);
		DictionaryCache.getInstance ( ).putBizTypeInCache(biztype);
		return result;
	}
	
	public int deleteBizOption ( String bizOptionId , boolean absDel )
			throws Exception {
		BizOption bizoption = dictionaryDao.retrieveBizOption(bizOptionId);
		int result = dictionaryDao.deleteBizOption ( bizOptionId , absDel );
		BizType bizType = new BizType();
		bizType.setBizTypeKey(bizoption.getBizTypeKey());
		DictionaryCache.getInstance ( ).removeBizTypeFromCache(bizType);
		List ops = retrieveBizOptionList ( bizoption.getBizTypeKey ( ) , DictionaryCache.BIZOPTION_STATUS_ALL ) ;
		bizType.setOptions(ops);
		DictionaryCache.getInstance ( ).putBizTypeInCache(bizType);
		return result;
	}
	
	public int deleteBizType ( String bizTypeId ) throws Exception {
		// TODO Auto-generated method stub
		BizType  bizType = dictionaryDao.retrieveBizType ( bizTypeId ) ; 
//		List optionlist  = dictionaryDao.getBizOptionList(bizType.getBizTypeKey());
//		if (null !=optionlist || optionlist.size() > 0 ) {
//			for ( int i =0 ;i<optionlist.size();i++ ) {
//				Map map = ((Map)optionlist.get(i));
//				BizOption  bizOption = new BizOption();
//				bizOption.setOptionId(map.get("OPTIONID").toString());
//				DictionaryCache.getInstance ( ).removeBizOptionFromCache(bizOption);
//			}
//		}
		dictionaryDao.deleteBizOptionByBizType ( bizType.getBizTypeKey ( ) ) ; 
		
		int result = dictionaryDao.deleteBizType ( bizType.getBizTypeId ( ) ) ;
		DictionaryCache.getInstance ( ).removeBizTypeFromCache(bizType);
		
		return result;
	}
	
	public int getBizCodeDesc ( BizEntity bizEntity ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.getBizCodeDesc ( bizEntity );
	}
	
	public int getBizCodeDescInEntityList ( List bizEntityList )
			throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.getBizCodePowerInEntityList ( bizEntityList );
	}
	
	public int getBizCodePower ( BizEntity bizEntity ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.getBizCodePower ( bizEntity );
	}
	
	public int getBizCodePowerInEntityList ( List bizEntityList )
			throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.getBizCodePowerInEntityList ( bizEntityList );
	}
	
	public void refreshBizDictionary ( ) throws Exception  {
		// TODO Auto-generated method stub
		dictionaryDao.refreshBizDictionary ( );
	}
	
	public BizOption retrieveBizOption ( String bizOptionId ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.retrieveBizOption ( bizOptionId );
	}
	
	public List retrieveBizOptionList ( String bizTypeKey , String status )
			throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.retrieveBizOptionList ( bizTypeKey , status );
	}
	
	public BizType retrieveBizType ( String bizTypeId ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.retrieveBizType ( bizTypeId );
	}
	
	public List retrieveBizTypeList ( ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.retrieveBizTypeList ( );
	}
	/*
	public synchronized int saveBizType ( BizType bizType ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.saveBizType ( bizType );
	}
	*/
	
	public int saveBizType ( BizType bizType ) throws Exception {
		int returnValue = 0;
		String bizTypeId = bizType.getBizTypeId ( );
		if ( Validator.isNull ( bizTypeId ) ) {
			//insert ( bizType , "insertBizType" );
			dictionaryDao.saveBizType ( bizType ) ;
			DictionaryCache.getInstance ( ).putBizTypeInCache ( bizType );
		}
		else {
			BizType olBizType = retrieveBizType ( bizTypeId ) ;  
			List ops = retrieveBizOptionList ( olBizType.getBizTypeKey ( ) , DictionaryCache.BIZOPTION_STATUS_ALL ) ;
			if ( !olBizType.getBizTypeKey ( ) .equals ( bizType.getBizTypeKey ( ) ) ) {
				dictionaryDao.updateBizType ( bizType ) ;
				for ( java.util.Iterator iter = ops.iterator ( ) ; iter.hasNext ( ) ;  ) {
					BizOption op = ( BizOption ) iter.next ( );
					op.setBizTypeKey ( bizType.getBizTypeKey ( ) ) ;
					dictionaryDao.updateBizOption ( op ) ; 
				}
				BizType newBizType = dictionaryDao.getBizTypeByKey( bizType.getBizTypeKey() );
				newBizType.setOptions ( ops );
				DictionaryCache.getInstance ( ).putBizTypeInCache ( newBizType );
				DictionaryCache.getInstance ( ).removeBizTypeFromCache( olBizType );
			} else {
				dictionaryDao.updateBizType ( bizType ) ;
				bizType.setOptions ( ops ) ;
				DictionaryCache.getInstance ( ).modifyBizTypeInCache ( bizType );
			}
		}
		return returnValue;
	}	
	
	public int updateBizOption ( BizOption bizOption ) throws Exception {
		// TODO Auto-generated method stub
		int result = dictionaryDao.updateBizOption ( bizOption );
		List ops = retrieveBizOptionList ( bizOption.getBizTypeKey ( ) , DictionaryCache.BIZOPTION_STATUS_ALL ) ;
		BizType biztype = dictionaryDao.getBizTypeByKey(bizOption.getBizTypeKey());
		biztype.setOptions(ops);
		DictionaryCache.getInstance ( ).putBizTypeInCache(biztype);
		return result;
	}
	
	public boolean validateOption ( BizOption option ) throws Exception {
		List list = dictionaryDao.getBizOptionRepeatList ( option );
		if ( list != null && list.size ( ) > 0 ) return true;
		else return false ; 
	}
	
	public boolean validateType ( BizType type ) throws Exception {
		List list = dictionaryDao.getBizTypeRepeatList ( type );
		if ( list != null && list.size ( ) > 0 ) return true;
		else return false ; 
	}

	public Long countBizOption(BizOption option) throws CheckException {
		return dictionaryDao.countBizOption(option);
	}
	
	public Long countBizOptionValue(BizOption option) throws CheckException {
		return dictionaryDao.countBizOptionValue ( option );
	}

	public Long countBizType(String biztypekey) throws CheckException {
		return dictionaryDao.countBizType(biztypekey);
	}

	public List retrieveBizTypeList ( BizType bizType ) throws Exception {
		// TODO Auto-generated method stub
		return dictionaryDao.retrieveBizTypeList ( bizType );
	}
	
}
